#include "moduleplay.h"

ModulePlay::ModulePlay(QObject *parent)
    : QObject(parent)
{
    init();
}

ModulePlay::~ModulePlay()
{
    if (m_playTimer->isActive())
    {
        m_playTimer->stop();
    }

    delete m_playTimer;
    delete m_mediaList;
    delete m_pixLabel;
    delete m_bulletin;
    delete m_flashPlayer;

}

void ModulePlay::hide()
{
    //此处顺序不能错误,要先m_playTimer暂停之后再把其它播放隐藏,不然可能出现
    //同时2个视频播放的错误
//    m_layoutStruct.partitionID = -1;

    m_playTimer->stop();
    if (m_mediaList->isRunning())
    {
        m_mediaList->closePlay();
    }
    m_mediaList->hide();
    m_pixLabel->hide();
    m_bulletin->hide();
    m_flashPlayer->hide();

#ifdef PDFVIEWER
    m_pdfViewer->hide();
#endif
}

void ModulePlay::show()
{
}

void ModulePlay::stop()
{
    m_playTimer->stop();
}

//线路信息，即播放相对应的播放布局和播放内容
void ModulePlay::updateRoutePkg(const RoutePkgStruct& routePkgStruct)
{
    m_cntList.clear();
    m_layoutStruct = routePkgStruct.layoutStruct;
    m_cntList = routePkgStruct.contentStructList;

//    for(int i = 0; i < m_cntList.count(); i++)
//    {
//        qDebug() << "ModulePlay>>" << i << m_cntList.at(i).duration << m_cntList.count() << m_layoutStruct.partitionID << m_layoutStruct.width << m_layoutStruct.height << i <<"cndList----->" << m_cntList.at(i).contentFile;
//    }
    m_nCur = 0;
    m_onlyPlayUnlimited = false;
}

void ModulePlay::init()
{
    m_playTimer = new QTimer(this);
    m_playTimer->setSingleShot(true);
    connect(m_playTimer, SIGNAL(timeout()), this, SLOT(PlayToNext()));

    m_nCur = 0;
    m_onlyPlayUnlimited = false;

    m_mediaList = new medialist(1, (QWidget *)this->parent());
    connect(m_mediaList, SIGNAL(mplayerTimePos(float)), this, SLOT(receTimePos(float)));
    connect(m_mediaList, SIGNAL(mediaPlayEnd()),        this, SLOT(mediaPlayEnd()));

    m_pixLabel = new PixPlayLabel(2, (QWidget *)this->parent());
    m_bulletin = new bulletinlist(3, (QWidget *)this->parent());
    m_flashPlayer = new FlashPlayer((QWidget *)this->parent());

    //因为第一次显示播放flash会出现异常（即播放的时候只播放成很小），所以要先在此处show一次，后面就正常显示了
    connect(m_flashPlayer, SIGNAL(msgFinish()), this, SLOT(flashLoadFinish()));

    m_bufferTimer = new QTimer(this);
    m_bufferTimer->setSingleShot(true);
    connect(m_bufferTimer, SIGNAL(timeout()), this, SLOT(toHideOther()));
#ifdef PDFVIEWER
    m_pdfViewer = new PdfViewer((QWidget *)this->parent());
    m_pdfViewer->hide();
#endif

}

//得到正在播放的类型
int ModulePlay::getCurPlayType() const
{
    return m_curPlayType;
}

//得到当前版块号
int ModulePlay::getCurPartitiionID() const
{
    return m_layoutStruct.partitionID;
}

void ModulePlay::play()
{
    int duration;
    m_onlyPlayUnlimited = false;
    if (m_layoutStruct.layoutID == -1 && m_layoutStruct.width == -1 && m_layoutStruct.height == -1)
    {
        return;
    }

    if (m_cntList.count() == 0)
    {
        this->hide();
        return;

    }

    ContentStruct cnt = m_cntList.at(m_nCur);
//    for(int i = 0; i < m_cntList.count(); i++)
//    {
//        int tyep = m_cntList.at(i).contentTypeID;
//    }

    if (cnt.contentFile.isEmpty())
    {
        m_nCur++;
        if (m_nCur >= m_cntList.count())
        {
            m_nCur = 0;
        }
         cnt = m_cntList.at(m_nCur);         
    }

    while(cnt.duration == 0)
    {
        m_nCur++;
        if (m_nCur >= m_cntList.count())
        {
            this->PlayToNext();
            return;
        }
         cnt = m_cntList.at(m_nCur);
    }

    //如果播放时间设置为0的话,那么强制给他10秒的播放时间
    if (cnt.duration <= 0)
    {
        cnt.duration = 3;
    }

//    duration = (cnt.duration+1) * 1000;

    duration = (cnt.duration) * 1000;

    if (duration <= 0)
        duration = 10 * 1000;


//#define CONTENT_MEDIA   1
//#define CONTENT_PIC     2
//#define CONTENT_TEXT    3
//#define CONTENT_FLASH   4

    //把显示宽度和屏幕宽度进行相应的比例缩小
#ifdef _TTY_WIN_
    QDesktopWidget* desktop = QApplication::desktop();
    double width = m_layoutStruct.width * desktop->width() / GLOBAL_RESOLUTION_WIDTH;
    double x = m_layoutStruct.x * desktop->width() / GLOBAL_RESOLUTION_WIDTH;
    m_layoutStruct.width = width;
    m_layoutStruct.x = x;
#endif


    RoutePkgStruct routePkg;
    routePkg.layoutStruct = m_layoutStruct;
    routePkg.contentStructList.append(cnt);

    m_curPlayType = cnt.contentTypeID;
    m_playTimer->start(duration);

//    qDebug() << "m_curPlayType====>" << m_curPlayType << cnt.contentFile;

    switch (cnt.contentTypeID)
    {
    case CONTENT_MEDIA:
//        qDebug() << routePkg.contentStructList.at(0).contentFile << "CONTENT_MEDIA ===============================================>" << routePkg.layoutStruct.width << routePkg.layoutStruct.height;
        if (m_mediaList->isRunning())
        {
            m_mediaList->closePlay();
            m_mediaList->updateList(routePkg);
        } else {
            m_mediaList->updateList(routePkg);
        }
        m_mediaList->show();
        m_pixLabel->hide();
        m_bulletin->hide();
        m_flashPlayer->hide();

#ifdef PDFVIEWER
        m_pdfViewer->hide();
#endif

        break;
    case CONTENT_PIC:
//        qDebug() << m_layoutStruct.x << m_layoutStruct.y << m_layoutStruct.width << m_layoutStruct.height << "CONTENT_PIC ===============================================>" << routePkg.contentStructList.at(0).contentFile;
        m_pixLabel->setGeometry(m_layoutStruct.x,
                         m_layoutStruct.y,
                         m_layoutStruct.width,
                         m_layoutStruct.height);
        m_pixLabel->setPlayCntList(routePkg.contentStructList);
        m_pixLabel->show();
        if (m_mediaList->isRunning())
        {
            m_mediaList->closePlay();
        }
        m_mediaList->hide();
        m_bulletin->hide();
        m_flashPlayer->hide();

#ifdef PDFVIEWER
        m_pdfViewer->hide();
#endif

        m_pixLabel->start();
        break;
    case CONTENT_TEXT:
//        qDebug() << "CONTENT_TEXT ===============================================>"  << routePkg.contentStructList.at(0).contentFile;
        m_bulletin->updateList(routePkg);
        m_bulletin->show();
        m_mediaList->hide();
        if (m_mediaList->isRunning())
        {
            m_mediaList->closePlay();
        }
        m_mediaList->hide();
        m_pixLabel->hide();
        m_flashPlayer->hide();

#ifdef PDFVIEWER
        m_pdfViewer->hide();
#endif
        break;
    case CONTENT_FLASH:
//        qDebug() << cnt.contentFile << "FLASH ==start=============================================>"  << m_layoutStruct.width << m_layoutStruct.height;
        m_flashPlayer->setRect(m_layoutStruct.x,
                         m_layoutStruct.y,
                         m_layoutStruct.width,
                         m_layoutStruct.height);
        m_flashPlayer->open(cnt.contentFile);

//        m_mediaList->show();
//        m_pixLabel->hide();
//        m_bulletin->hide();
        break;
    case CONTENT_PDF:
//        qDebug() << cnt.contentFile << "CONTENT_PDF ==start=============================================>"  << m_layoutStruct.width << m_layoutStruct.height;

#ifdef PDFVIEWER
        m_pdfViewer->setGeometry(m_layoutStruct.x, m_layoutStruct.y,
                                 m_layoutStruct.width, m_layoutStruct.height);
        m_pdfViewer->setDocument(cnt.contentFile);
        m_pdfViewer->show();
#endif
        m_bulletin->hide();
        if (m_mediaList->isRunning())
        {
            m_mediaList->closePlay();
        }
        m_mediaList->hide();
        m_pixLabel->hide();
        m_flashPlayer->hide();
        break;
    case CONTENT_OnlyShowTxt:
//                qDebug() << "CONTENT_OnlyShowTxt ===============================================>"  << routePkg.contentStructList.at(0).contentFile;
        m_bulletin->onlyShowText(routePkg);
        m_bulletin->show();
        m_mediaList->hide();
        if (m_mediaList->isRunning())
        {
            m_mediaList->closePlay();
        }
        m_mediaList->hide();
        m_pixLabel->hide();
        m_flashPlayer->hide();

        #ifdef PDFVIEWER
                m_pdfViewer->hide();
        #endif
        break;
    default:
        break;
    }
}

void ModulePlay::startToUpdata(const QString& msg)
{
    m_bulletin->updateDateState(msg);
    m_bulletin->show();

    if (m_mediaList->isRunning())
    {
        m_mediaList->closePlay();
        m_mediaList->stop();
    }
    m_mediaList->hide();
    m_pixLabel->hide();
    m_flashPlayer->hide();
    m_playTimer->stop();

//    m_bulletin->stop();

//    qDebug() << "==================<<>>> updateDataState <<<==================+";
    ((QWidget *)this->parent())->repaint();
}

void ModulePlay::updateDataState(const QString &msg)
{
    m_bulletin->updateDateState(msg);
    m_bulletin->show();

    if (m_mediaList->isRunning())
    {
        m_mediaList->closePlay();
        m_mediaList->stop();
    }
    m_mediaList->hide();
    m_pixLabel->hide();
    m_flashPlayer->hide();
    m_playTimer->stop();

//    qDebug() << "==================<<>>> updateDataState <<<==================+";
    ((QWidget *)this->parent())->repaint();
}


void ModulePlay::seekToTimePos(const float seconds)
{
    if (m_curPlayType == CONTENT_MEDIA)
    {
        m_mediaList->seekToTimePos(seconds);
    }
}

void ModulePlay::getCurTimePos()
{
    if (m_curPlayType == CONTENT_MEDIA)
    {
        m_mediaList->getCurTimePos();
    }
}

void ModulePlay::PlayToNext()
{
//    qDebug() << "PlayToNext======================================>>>";

    int duration;       //播放时间
    m_nCur++;

    if (m_nCur >= m_cntList.count())
    {
        m_onlyPlayUnlimited = true;
        m_nCur = 0;
    } else {
        while(m_cntList.at(m_nCur).duration == 0)
        {
            m_nCur++;
            if (m_nCur >= m_cntList.count())
            {
                m_onlyPlayUnlimited = true;
                m_nCur = 0;
                break;
            }
        }

    }


    if (m_onlyPlayUnlimited)
    {
        bool hadUnlimitCnt = false;

        for(int i = m_nCur; i < m_cntList.count(); i++)
        {
//            qDebug() << m_cntList.at(i).contentFile << m_cntList.at(i).isUnlimited;
            if (m_cntList.at(i).isUnlimited && m_cntList.at(i).duration != 0)
            {
                m_nCur = i;
                hadUnlimitCnt = true;
                break;
            }
        }
        if (!hadUnlimitCnt)
        {
            m_onlyPlayUnlimited = false;
            emit playAllFinish(m_layoutStruct.partitionID);
            m_nCur = 0;
//            return;
        }
    }


//    if (m_layoutStruct.partitionID == 1)
//        qDebug() << "aaam_cntList========" << m_cntList.at(m_nCur).isUnlimited << m_cntList.at(m_nCur).contentFile;

    if (m_nCur >= m_cntList.count())
    {
        this->hide();
        return;
    }

    ContentStruct cnt = m_cntList.at(m_nCur);

    if (cnt.contentFile.isEmpty())
    {
        m_nCur++;
        if (m_nCur >= m_cntList.count())
        {
            m_nCur = 0;
        }
        cnt = m_cntList.at(m_nCur);
    }


//    QString str;
//    QTextCodec *gbk = QTextCodec::codecForName("UTF-8");
//    str = QString::fromUtf8(cnt.contentFile.toLatin1().data());
//    str = gbk->fromUnicode(str.toLatin1());
//    str = QString::fromLocal8Bit(str.toLocal8Bit());

    //如果播放时间设置为0的话,那么强制给他10秒的播放时间

    if (cnt.duration <= 0)
    {
        cnt.duration = 3;
    }

//    duration = (cnt.duration+1) * 1000;
    duration = (cnt.duration) * 1000;

    if (duration <= 0)
        duration = 10 * 1000;

//#define CONTENT_MEDIA   1
//#define CONTENT_PIC     2
//#define CONTENT_TEXT    3
//#define CONTENT_FLASH   4

    RoutePkgStruct routePkg;
    routePkg.layoutStruct = m_layoutStruct;
    routePkg.contentStructList.append(cnt);
    m_playTimer->start(duration);

    m_curPlayType = cnt.contentTypeID;

    switch (cnt.contentTypeID)
    {
    case CONTENT_MEDIA:
//        qDebug() << "CONTENT_MEDIA ===============================================>" << duration << routePkg.contentStructList.count()  << routePkg.contentStructList.at(0).contentFile;;
        if (m_mediaList->isRunning())
        {
            m_mediaList->closePlay();
        }
        m_mediaList->nextToPlay(routePkg);
        m_mediaList->show();
        m_pixLabel->hide();
        m_bulletin->hide();
        m_flashPlayer->hide();

#ifdef PDFVIEWER
        m_pdfViewer->hide();
#endif

        break;
    case CONTENT_PIC:
//        qDebug() << "CONTENT_PIC ===============================================>" << routePkg.contentStructList.at(0).contentFile;
        m_pixLabel->setGeometry(m_layoutStruct.x,
                         m_layoutStruct.y,
                         m_layoutStruct.width,
                         m_layoutStruct.height);
        m_pixLabel->setPlayCntList(routePkg.contentStructList);

        m_pixLabel->show();
        if (m_mediaList->isRunning())
        {
            m_mediaList->closePlay();
        }
        m_mediaList->hide();
        m_bulletin->hide();
        m_flashPlayer->hide();

#ifdef PDFVIEWER
        m_pdfViewer->hide();
#endif

        m_pixLabel->start();
        break;
    case CONTENT_TEXT:
//        qDebug() << "NextToPlay CONTENT_TEXT ===============================================>"  << routePkg.contentStructList.at(0).contentFile;
        m_bulletin->updateList(routePkg);
        m_bulletin->show();
        if (m_mediaList->isRunning())
        {
            m_mediaList->closePlay();
        }
        m_mediaList->hide();
        m_pixLabel->hide();
        m_flashPlayer->hide();

#ifdef PDFVIEWER
        m_pdfViewer->hide();
#endif

        break;
    case CONTENT_FLASH:
//        qDebug() << cnt.contentFile << "FLASH ===next to play============================================>"  << routePkg.contentStructList.at(0).contentFile;
        m_flashPlayer->setRect(m_layoutStruct.x,
                         m_layoutStruct.y,
                         m_layoutStruct.width,
                         m_layoutStruct.height);
        m_flashPlayer->open(cnt.contentFile);
        break;
    case CONTENT_PDF:

#ifdef PDFVIEWER
        m_pdfViewer->setGeometry(m_layoutStruct.x, m_layoutStruct.y,
                                 m_layoutStruct.width, m_layoutStruct.height);
        m_pdfViewer->setDocument(cnt.contentFile);
#endif

        m_bulletin->hide();
        if (m_mediaList->isRunning())
        {
            m_mediaList->closePlay();
        }
        m_mediaList->hide();
        m_pixLabel->hide();
        m_flashPlayer->hide();

#ifdef PDFVIEWER
        m_pdfViewer->show();
#endif
        break;
    default:
        break;
    }
}


void ModulePlay::flashLoadFinish()
{
//    qDebug() << "======================== flashLoadFinish ===================";

    m_bufferTimer->start(200);
}

void ModulePlay::toHideOther()
{
//    qDebug() << "toHideOther >>>>>>>>>>>>>>>>>>>>>>>>>>>";
//    m_flashPlayer->setRect(m_layoutStruct.x,
//                     m_layoutStruct.y,
//                     m_layoutStruct.width,
//                     m_layoutStruct.height);
    m_flashPlayer->show();
    m_bulletin->hide();
    m_mediaList->hide();
    m_pixLabel->hide();

#ifdef PDFVIEWER
    m_pdfViewer->hide();
#endif
}

void ModulePlay::updateConfig()
{
    Configclass config;
    int scrollSpeed = config.getScrollSpeed();
    m_bulletin->setScrollSpeed(scrollSpeed);
}

//发送当前版块号和时间进度
void ModulePlay::receTimePos(const float seconds)
{
    emit mplayerTimePos(seconds, m_layoutStruct.partitionID);
}

void ModulePlay::mediaPlayEnd()
{
    if (m_nCur < m_cntList.count())
    {
        ContentStruct cnt = m_cntList.at(m_nCur);

        if (cnt.contentTypeID == CONTENT_MEDIA)
        {
            RoutePkgStruct routePkg;
            routePkg.layoutStruct = m_layoutStruct;
            routePkg.contentStructList.append(cnt);
            m_mediaList->closePlay();
            m_mediaList->nextToPlay(routePkg);
            m_mediaList->show();
        }

    }
}
